﻿using System;
using System.ComponentModel;
using System.Windows;
namespace WPFExcelReport
{
    /// <summary>
    /// 专用于布卷剪布
    /// </summary>
    public partial class TWBQI03
    { 
        public TWBQI03()
        {
            if (DesignerProperties.GetIsInDesignMode(this))
                return;
            MV = new VWBBST();
            this.DataContext = MV;
            InitializeComponent();
            IsLogOn = true;
        }
        public override void vwMaster_CurrentChanged(object sender, EventArgs e)
        {
            base.vwMaster_CurrentChanged(sender, e);
            RWAUF =TMV.WBBSTKNRK;
            LOOMID = TMV.WBBKOMMNR;
        }
        public override void OnEXAUFChg(string vlaue)
        {
            TBMain.Clear();
            if(EXAUF.Length == 0)
                return;
            if(IsMaster)
            {
                if(VAR.Trim().Length==0&& ART.Trim().Length==0)
                {
                    MessageBox.Show("必须输入布卷号的前10位,例如2016080428，或品种编号");
                    return;
                }
            }
            dwData = TBMain.NewRow();
            dwData["WBBMANDANT"] = app.Mandant;
            dwData["WBBWMNRKRS"] = EXAUF.Substring(0, 2);
            dwData["WBBWMLFDNR"] = EXAUF.Substring(2, 4);
            dwData["WBBWMPOS"] =  EXAUF.Substring(6, 2);
            dwData["WBBNEUBEN"] = EXAUF.Substring(20, 8); 
            Helpers.Helper.GetSTime(ref TMV.date, ref TMV.time, ref TMV.stime);
            string yesterday = Helpers.Helper.GetYesterday();
            string qstr = "";
            switch(app.company)
            {
                default:
                    if (double.Parse(TMV.stime) < 800)
                    {
                        qstr = " AND WBEWBUCHDAT||SUBSTR('00000000'||TO_CHAR(WBEWBUCHUHR),-8)>='" + yesterday + "20000000' AND WBEWBUCHDAT||SUBSTR('00000000'||TO_CHAR(WBEWBUCHUHR),-8)<='" + TMV.date + "08000000'";

                    }
                    else if (double.Parse(TMV.stime) > 2000)
                    {
                        qstr = " AND WBEWBUCHDAT||SUBSTR('00000000'||TO_CHAR(WBEWBUCHUHR),-8)>='" + TMV.date + "20000000'";
                    }
                    else
                    {
                        qstr = " AND WBEWBUCHDAT='" + TMV.date + "'";
                    }
                   
                    break;
            }
            if (checkSure.IsChecked==true)
            {
                
                if (txtSTART.Text.Trim().Length!=0)
                {
                    if (txtSTART1.Text.Trim().Length == 0)
                    {
                        txtSTART1.Text = "0000";
                    }
                    if (txtEND.Text.Trim().Length==0)
                    {
                        txtEND.Text = TMV.date.ToString().Trim();
                    }
                    if (txtEND1.Text.Trim().Length==0)
                    {
                        txtEND1.Text = TMV.stime.ToString().Trim();
                    }
                   
                }
                if (txtSTART.Text.Trim().Length != 8 || txtSTART1.Text.Trim().Length != 4 || txtEND.Text.Trim().Length != 8 || txtEND1.Text.Trim().Length != 4)
                {
                    MessageBox.Show("日期或时间格式不对，20161205，0801代表2016年12月5日，8点01分"); return;
                }

                qstr = " AND WBEWBUCHDAT||SUBSTR('00000000'||TO_CHAR(WBEWBUCHUHR),-8)>='" + txtSTART.Text.ToString().Trim()+txtSTART1.Text.ToString().Trim() + "0000' AND WBEWBUCHDAT||SUBSTR('00000000'||TO_CHAR(WBEWBUCHUHR),-8)<='" + txtEND.Text.ToString().Trim()+txtEND1.Text.ToString().Trim() + "0000'";
            }
            if (IsMaster)
            {
                qstr += "  AND X.WBBART LIKE '" +ART.Trim()+ "%' AND X.WBBSTKNR LIKE'" + VAR + "%'";
            }
            else
            { 
                qstr +="  AND X.WBBWMNRKRS ='"+ EXAUF.Substring(0, 2)+"' AND X.WBBWMLFDNR ='"+EXAUF.Substring(2, 4)+"' AND X.WBBWMPOS ='"+ EXAUF.Substring(6, 2)+"' AND WBEWBUCHBEN ='"+ EXAUF.Substring(20, 8) + "'";
            }
            dwData["WBBFILLER"] = qstr;
            MergeData(TMV.QQI03(dwData),false);
            cWBLIST.IsExpanded = true;
            base.OnEXAUFChg(vlaue);
        }
        public override void QueryCommand(object sender, RoutedEventArgs e)
        {
            if (app.User.Trim() == "YBGL")
                IsMaster = true;
            TMV.CUSER = app.User;
            EXAUF = "";
            EXAUF = MACHINEID.Substring(0,8).PadRight(20) + TMV.CUSER.PadRight(10) + TMV.date.PadRight(10);
            base.QueryCommand(sender, e);
        }

        

        private void btnSure_Click(object sender, RoutedEventArgs e)
        {
            if (checkSure.IsChecked==false)
            {
                checkSure.IsChecked = true;
            }
            else
            {
                checkSure.IsChecked = false;
            }
        }

        private void btnDelete_Click(object sender, RoutedEventArgs e)
        {
            if(dwMaster==null)
            {
                MessageBox.Show("请确认选中的记录");
                return;
            }
            if ( MessageBox.Show("清确认是否需要删除当前布卷，一旦删除将无法撤销此操作，不能找回改布卷，请谨慎操作！","删除提示！",MessageBoxButton.OKCancel,MessageBoxImage.Warning)==MessageBoxResult.OK)
            { 
                PM["RWKRS"] = TMV.WBBRWKRS;
                PM["RWLFD"] = TMV.WBBRWLFD;
                PM["RWPOS"] = TMV.WBBPOS;
                TMV.DeleteWB(PM);
                MessageBox.Show("删除成功");
            }
        }

        private void LogOnAdminCommand(object sender, RoutedEventArgs e)
        {
            IsLogOn = false;
        }

        private void FlipFlopCommand(object sender, RoutedEventArgs e)
        {
            cWBLIST.IsExpanded = !cWBLIST.IsExpanded;
        }
    }
}
